package com.example.ibsp_demo.controller.create;

import com.example.ibsp_demo.mapper.content.CommentMapper;
import com.example.ibsp_demo.pojo.entity.content.Comment;
import com.example.ibsp_demo.pojo.entity.content.Like;
import com.example.ibsp_demo.pojo.response.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.example.ibsp_demo.mapper.content.*;



import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/create")
@CrossOrigin(origins = "http://localhost:8080")
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;


    @Autowired
    private ContentMapper contentMapper;

    @Autowired
    private LikeMapper likeMapper;


    @GetMapping("/comments")
    public ResponseMessage<List<Comment>> getComments(@RequestParam Integer ctid) {
        List<Comment> comments = commentMapper.selectByCtid(ctid);
        return ResponseMessage.success(comments);
    }

    //评论
    @PostMapping("/comment")
    public ResponseMessage<String> commentContent(
            @RequestParam Integer commenterId,
            @RequestParam Integer ctid,
            @RequestParam String content) {
        try {
            Comment comment = new Comment();
            comment.setCtid(ctid);
            comment.setCommenterId(commenterId);
            comment.setContent(content);
            comment.setCreateTime(LocalDateTime.now());
            comment.setParentId(null); // 主评论
            comment.setLikeCount(0);

            commentMapper.insert(comment);

            // 更新 content 表的评论数
            contentMapper.updateCommentCount(ctid, 1); // +1

            return ResponseMessage.success("评论成功");
        } catch (Exception e) {
            return ResponseMessage.error(500, "评论失败: " + e.getMessage());
        }
    }

    //评论回复
    @PostMapping("/reply")
    public ResponseMessage<String> replyToComment(
            @RequestParam Integer commenterId,
            @RequestParam Integer ctid,
            @RequestParam Integer parentId,
            @RequestParam String content) {
        try {
            Comment comment = new Comment();
            comment.setCtid(ctid);
            comment.setCommenterId(commenterId);
            comment.setContent(content);
            comment.setCreateTime(LocalDateTime.now());
            comment.setParentId(parentId); // 设置父评论ID
            comment.setLikeCount(0);

            commentMapper.insert(comment);

            return ResponseMessage.success("回复成功");
        } catch (Exception e) {
            return ResponseMessage.error(500, "回复失败: " + e.getMessage());
        }
    }

    @PostMapping("/comment/like")
    public ResponseMessage<String> likeComment(@RequestParam Integer likerId,
                                               @RequestParam Integer commentId) {
        // 判断是否已点赞该评论
        Like existing = likeMapper.selectByLikerAndComment(likerId, commentId);
        if (existing != null) {
            return ResponseMessage.error(500, "您已点赞过该评论");
        }

        Comment comment = commentMapper.selectByCommentId(commentId);
        if (comment == null) {
            return ResponseMessage.error(500, "评论不存在");
        }

        Like like = new Like();
        like.setLikerId(likerId);
        like.setCtid(comment.getCtid()); // 保留内容ID（可选）
        like.setCommentId(commentId);
        like.setLikeTime(LocalDateTime.now());

        likeMapper.likeComment(like);
        commentMapper.updateLikeCount(commentId, 1);

        return ResponseMessage.success("评论点赞成功");
    }

    //取消评论点赞
    @PostMapping("/comment/unlike")
    public ResponseMessage<String> unlikeComment(@RequestParam Integer likerId,
                                                 @RequestParam Integer commentId) {
        likeMapper.cancelLikeByLikerAndComment(likerId, commentId);
        commentMapper.updateLikeCount(commentId, -1); // 减少点赞数
        return ResponseMessage.success("取消评论点赞成功");
    }

    @GetMapping("/checkLike")
    public ResponseMessage<Boolean> checkLikeStatus(@RequestParam Integer uid, @RequestParam Integer ctid) {
        // 示例逻辑：检查用户是否点赞
        Like like = likeMapper.selectByLikerAndContent(uid, ctid);
        return ResponseMessage.success(like != null);
    }
}

